package com.campus.service.impl;

import com.campus.constant.MessageConstant;
import com.campus.constant.StatusConstant;
import com.campus.context.CurrentHolder;
import com.campus.exception.DeletionNotAllowedException;
import com.campus.exception.ResourceNotFoundException;
import com.campus.mapper.LabEquipmentMapper;
import com.campus.pojo.dto.LabEquipmentDTO;
import com.campus.pojo.dto.LabEquipmentPageQueryDTO;
import com.campus.pojo.entity.LabEquipment;
import com.campus.pojo.vo.LabEquipmentVO;
import com.campus.result.PageResult;
import com.campus.service.LabEquipmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class LabEquipmentServiceImpl implements LabEquipmentService {

    @Autowired
    private LabEquipmentMapper labEquipmentMapper;

    /**
     * 新增实验室设备
     *
     * @param labEquipmentDTO 实验室设备信息
     */
    @Override
    public void save(LabEquipmentDTO labEquipmentDTO) {
        // 1.构建entity对象-labEquipment
        LabEquipment labEquipment = LabEquipment.builder()
                .name(labEquipmentDTO.getName())
                .labId(labEquipmentDTO.getLabId())
                .status(StatusConstant.STATUS_NORMAL)
                .build();

        // 2.调用mapper方法
        labEquipmentMapper.insert(labEquipment);
    }

    /**
     * 批量删除实验室设备
     *
     * @param ids 设备id集合
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        // 1.是否处于被预约状态
        for (Long id : ids) {
            LabEquipment labEquipment = labEquipmentMapper.getById(id);
            if (labEquipment.getStatus() == StatusConstant.STATUS_RESERVED) {
                throw new DeletionNotAllowedException(MessageConstant.EQUIPMENT_IS_RESERVED);
            }
        }

        // 2.调用mapper方法
        labEquipmentMapper.deleteByIds(ids);
    }

    /**
     * 分页查询实验室设备
     *
     * @param labEquipmentPageQueryDTO 分页查询条件
     * @return 分页结果
     */
    @Override
    public PageResult<LabEquipmentVO> pageQuery(LabEquipmentPageQueryDTO labEquipmentPageQueryDTO) {
        // 1.设置分页参数
        PageHelper.startPage(labEquipmentPageQueryDTO.getCurrentPage(), labEquipmentPageQueryDTO.getPageSize());

        // 2.调用mapper层
        Page<LabEquipmentVO> page = labEquipmentMapper.pageQuery(labEquipmentPageQueryDTO);

        // 3.返回结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 更新设备状态
     *
     * @param id     设备id
     * @param status 状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        // 1.查询设备是否存在
        LabEquipment labEquipment = labEquipmentMapper.getById(id);
        if (labEquipment == null) {
            // 不存在
            throw new ResourceNotFoundException(MessageConstant.EQUIPMENT_NOT_FOUND);
        }

        // 2.构建entity对象-LabEquipment
        //如果要改为正常状态即无预约 此时需将userId置为null
        if (status == StatusConstant.STATUS_NORMAL) {
            labEquipment = LabEquipment.builder()
                    .id(id)
                    .userId(null)
                    .status(status)
                    .build();
        } else {
            labEquipment = LabEquipment.builder()
                    .id(id)
                    .userId(CurrentHolder.getCurrentId())
                    .status(status)
                    .build();
        }


        // 3.调用mapper方法
        labEquipmentMapper.update(labEquipment);
    }

    /**
     * 根据id查询设备
     *
     * @param id 设备id
     * @return 设备信息
     */
    @Override
    public LabEquipment getById(Long id) {
        return labEquipmentMapper.getById(id);
    }

    /**
     * 修改实验室设备信息
     *
     * @param labEquipmentDTO 设备信息
     */
    @Override
    public void update(LabEquipmentDTO labEquipmentDTO) {


        // 1.构建entity对象-LabEquipment 以便update方法复用
        LabEquipment labEquipment = LabEquipment.builder()
                .id(labEquipmentDTO.getId())
                .name(labEquipmentDTO.getName())
                .labId(labEquipmentDTO.getLabId())
                .build();

        // 2.调用mapper方法
        labEquipmentMapper.update(labEquipment);
    }
}
